/* 
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

function showMsg(){
    alert("hola mundo!");
}

function showModalMessage(msg) {
    $( "#dialog-message" ).html(msg);
    $( "#dialog-message" ).dialog({
        modal: true,
        buttons: {
            Ok: function() {
                $( this ).dialog( "close" );
            }
        }
    });
} 

function showAddModal() {
    $('#stockForm').trigger("reset");
    
    $( "#popup-add" ).dialog({
        resizable: false,
        modal: true,
        buttons: {
            "Save": function() {
                $( this ).dialog( "close" );
                xAddStock();
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        }
    });
}

function xAddStock(){
                                
    //urlAction = $("#stockForm").attr('action');//window.location.protocol+"//"+window.location.host+"/StocksAnalysis/Stock/delete/"+stock_id;

    jQuery.ajax({
        type: "POST",
        url: "add",
        data: $("#stockForm").serialize(),
      success: function (response) {
        location.reload();
        showModalMessage("The Stock was added successfully");     
      },
      error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
      }
    });
}

function showUpdateModal(stock_symbol) {

    urlAction = "showStock/"+stock_symbol; //window.location.protocol+"//"+window.location.host+"/StocksAnalysis/Stock/showStock/"+stock_symbol;
    //alert(urlAction);
    
    jQuery.ajax({
       type: "GET",
       url: urlAction,
       success: function (response) {
         //jQuery("#usergrid").trigger("reloadGrid");
         //clear();
           $( "#popup-update-content" ).html(response);
           
           $( "#popup-update" ).dialog({
               resizable: false,
               modal: true,
               buttons: {
                   "Save": function() {
                       $( this ).dialog( "close" );
                       xSave(stock_Id);
                   },
                   Cancel: function() {
                       $( this ).dialog( "close" );
                   }
               }
           });
           
           
       },
       error: function (xhr, ajaxOptions, thrownError) {
         alert(xhr.status);
         alert(thrownError);
       }
     });

}

function showDeleteConfirmationModal(stock_Id) {
    $( "#dialog-confirm" ).dialog({
        resizable: false,
        height:280,
        modal: true,
        buttons: {
            "Delete": function() {
                $( this ).dialog( "close" );
                xDelete(stock_Id);
            },
            Cancel: function() {
                $( this ).dialog( "close" );
            }
        }
    });
}

function xDelete(stock_id){

    urlAction = window.location.protocol+"//"+window.location.host+"/StocksAnalysis/Stock/delete/"+stock_id;

    jQuery.ajax({
      type: "GET",
      url: urlAction,
      success: function (response) {
        //jQuery("#usergrid").trigger("reloadGrid");
        //clear();
        $("#row_"+stock_id).remove();
        showModalMessage();

      },
      error: function (xhr, ajaxOptions, thrownError) {
        alert(xhr.status);
        alert(thrownError);
      }
    });
}